﻿#include "stack.h"
#include <stdio.h>
#include <stdlib.h>

bool CreateStack(Stack *stack, int size){
	if (size <= 0)
		return false;
	stack->values = (double*)malloc(sizeof(double)*size);
	stack->top = -1;
	stack->maxTop = size - 1;
	return true;
}

bool IsEmpty(Stack* stack){
	if(stack==NULL)
		return true;//empty pointer
	if(stack->top==-1){
		return true;
	}else{
		return false;
	}
}

bool IsFull(Stack* stack){
	if(stack->top==stack->maxTop){
		return true;
	}else{
		return false;
	}
}

bool Top(Stack* stack, double* x){
	if(IsEmpty(stack))//empty stack return false
		return false;
	 *x=stack->values[stack->top];
	return true;
}

bool Push(Stack* stack, double x){
	if(IsFull(stack))
		return false;
	stack->values[++stack->top] = x;//top+1 and then assign x
	return true;
}

bool Pop(Stack* stack, double* x){
	if(IsEmpty(stack))//empty stack return false
		return false;
	 *x=stack->values[stack->top--];//assign x and then top-1
	return true;
}

void DisplayStack(Stack* stack){
	if(IsEmpty(stack)){
		printf("Empty Stack!\n");
		return;
	}//check if the stack is empty
	//printf("*********Display Stack*********\n");
	int i;
	for(i=0; i<=stack->top; i++){
		printf("%f\n",stack->values[i]);
	}//loop each data in stack
	//printf("*************End****************\n");
}

void DestroyStack(Stack** pstack){
	Stack* stack=*pstack;
	free(stack->values);//Frees the memory occupied by the stack values
	free(stack);//Frees the memory occupied by the stack struct
	*pstack=NULL;//Point the stack to NULL
}
